package com.zhdl.modules.spacer.web;

import com.zhdl.common.util.WebResult;
import com.zhdl.common.ytools.HexToBitTools;
import com.zhdl.hardware.plc.siemens.service.SiemensCmdSendService;
import com.zhdl.modules.spacer.ProcessTest;
import com.zhdl.modules.spacer.entity.SpacerParameters;
import com.zhdl.modules.spacer.service.SpacerParametersService;
import com.zhdl.modules.spacer.service.SpacerPlCControlService;
import com.zhdl.modules.tools.IntToHexUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping(path = "/spacerControl", produces = MediaType.APPLICATION_JSON_VALUE)
public class SpacerWebControl {
    @Resource
    SpacerPlCControlService spacerPlCControlService;
    @Resource
    SpacerParametersService spacerParametersService;
    @Resource
    SiemensCmdSendService siemensCmdSendService;
    @PostMapping("saveParameters")
    public WebResult<String> saveParameters(@RequestParam int address, @RequestParam float value) {
        siemensCmdSendService.siemensWriteValue(ProcessTest.plcIp, IntToHexUtil.shortToHex(address),IntToHexUtil.floatToHex(value));
        return WebResult.okResult("saveParameters Success!");
    }
    @PostMapping("saveStaticParameters")
    public WebResult<String> saveStaticParameters(@RequestParam int type, @RequestParam int length,@RequestParam int runModel) {
        /*
            remarks:
            modelId 型号 1 M16 2 M12
            rodLength 螺杆长度 保存实际螺杆长度
            runModel 运动模式 1,2,3,4
         */
        SpacerParameters spacerParameters = new SpacerParameters();
        spacerParameters.setModelId(type);
        spacerParameters.setRodLength(length);
        spacerParameters.setType(runModel);
        spacerParametersService.update(spacerParameters,1);

        /*
            写入 型号  寄存器地址 0003 字节 6
            写入 运动模式  寄存器地址 0005 字节 10
         */
        String typeHex = IntToHexUtil.shortToHex(type);
        String modelHex = IntToHexUtil.shortToHex(runModel);
        siemensCmdSendService.siemensWriteValues(ProcessTest.plcIp,"0003",typeHex);
        siemensCmdSendService.siemensWriteValues(ProcessTest.plcIp,"0005",modelHex);

        /*
            写入Z轴高度  暂时写死  2个寄存器
            z2寄存器地址  "0092"       z4寄存器地址  "0096"
         */
        if (length == 90){
            int z2 = 115;
            String s = IntToHexUtil.intToHex(z2);
            siemensCmdSendService.siemensWriteValues(ProcessTest.plcIp,"0092",s);
        } else if (length == 80) {
            int z4 = 115;
            String s = IntToHexUtil.intToHex(z4);
            siemensCmdSendService.siemensWriteValues(ProcessTest.plcIp,"0096",s);
        }

        return WebResult.okResult("saveStaticParameters Success!");
    }
    @GetMapping("controlSwitch")
    public WebResult<String> controlSwitch(@RequestParam int address, @RequestParam int bit,@RequestParam int value) {
        spacerPlCControlService.writePlcIO(address,bit,value==1);
        return WebResult.okResult("controlSwitch Success!");
    }

    @GetMapping("motorAbsRun")
    public WebResult<String> motorAbsRun(@RequestParam int model, @RequestParam float speed,@RequestParam float distance) {
        spacerPlCControlService.runAbsLocation(model,speed,distance);
        return WebResult.okResult("motorAbsRun Success!");
    }
    @GetMapping("motorJogRun")
    public WebResult<String> motorJogRun(@RequestParam int model, @RequestParam float speed) {
        spacerPlCControlService.runJog(model,speed);
        return WebResult.okResult("motorJogRun Success!");
    }
    @GetMapping("motorRelativeRun")
    public WebResult<String> motorRelativeRun(@RequestParam int model, @RequestParam float speed,@RequestParam float distance) {
        spacerPlCControlService.runRelativeLocation(model,speed,distance);
        return WebResult.okResult("motorAbsRun Success!");
    }
}
